package com.fangdada.demowebfulx2.client;

import com.fangdada.demowebfulx2.entity.User;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

/**
 * @Author: fd
 * @Date 2021/11/30 16:20
 * @Description:
 */
public class Client {

	public static void main(String[] args) {
		// 调用服务器地址
		WebClient webClient = WebClient.create("http://127.0.0.1:8082");

		// 根据 id 查询
		String id = "1";
		User userResult = webClient.get().uri("/users/{id}", id)
				.accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(User.class).block();
		System.out.println(userResult.getName());

		// 查询所有
		Flux<User> results = webClient.get().uri("/users")
				.accept(MediaType.APPLICATION_JSON).retrieve().bodyToFlux(User.class);
		results.map(User::getName)
				.buffer().doOnNext(System.out::println).blockFirst();
	}

}
